package com.heima.search.pojo;

import com.heima.common.util.JsonUtils;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.media.dtos.WmNewsContentItem;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Article Es 文档对象
 *
 * @author Fuqiang
 * @since :2023-03-31
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ArticleDoc {

    private Long id;
    private Date publishTime;
    private Integer layout;
    private String images;
    private String staticUrl;
    private String authorName;
    private String labels;
    private String channelName;
    private String title;
    private String content;

    public ArticleDoc(ArticleDto dto) {
        this.id = dto.getId();
        this.publishTime = dto.getPublishTime();
        this.layout = dto.getLayout();
        this.images = dto.getImages();
        this.staticUrl = dto.getStaticUrl();
        this.authorName = dto.getAuthorName();
        this.labels = dto.getLabels();
        this.channelName = dto.getChannelName();
        this.title = dto.getTitle();
        this.content = this.getDocContent(dto.getContent());
    }

    /**
     * 获取用来查询的es文档的Content 去除图片信息
     *
     * @param content
     * @return
     */
    private String getDocContent(String content) {

        List<WmNewsContentItem> wmNewsContentItems = JsonUtils.toList(content, WmNewsContentItem.class);
        if (CollectionUtils.isEmpty(wmNewsContentItems)) {
            return null;
        }
        return wmNewsContentItems.stream()
                .filter(item -> WmNewsContentItem.TYPE_TEXT.equals(item.getType()))
                .map(WmNewsContentItem::getValue).collect(Collectors.joining(","));
    }
}
